⚡️ Speed up method InjectPerfOnly.find_and_update_line_node by 24% in PR #769 (clean-async-branch)
#770
+65
−42
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #769
If you approve this dependent PR, these changes will be merged into the original PR branch
clean-async-branch.📄 24% (0.24x) speedup for
InjectPerfOnly.find_and_update_line_nodeincodeflash/code_utils/instrument_existing_tests.py⏱️ Runtime :
21.6 milliseconds→17.4 milliseconds(best of49runs)📝 Explanation and details
The optimization achieves a 24% speedup by targeting two key performance bottlenecks identified in the line profiler results:
1. Optimized
node_in_call_positionfunction (~22% faster):lineno,col_offset,end_lineno, andend_col_offsetonce usinggetattr()instead of repeatedly callinghasattr()and accessing attributes in the loopFalseimmediately if not anast.Callnode, avoiding unnecessary work2. Optimized
find_and_update_line_nodemethod (~18% faster):self.function_object.function_name,self.mode, etc.) in local variables to avoid repeated object attribute lookupsargslist incrementally usingextend()instead of creating multiple intermediate lists with unpacking operatorsast.walk()Performance gains are most significant for:
The optimizations maintain identical behavior while reducing CPU-intensive operations like attribute lookups and list operations that dominate the execution time in AST transformation workflows.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr769-2025-09-26T22.57.31and push.